<!DOCTYPE html>
<html>
   <head>
      {%FIELDS->isFieldEnabled a='bind' assign='bind_enabled'%}
      <link rel="StyleSheet" type="text/css" href="/tpa/admin.css" />
      <link rel="StyleSheet" type="text/css" href="/tpa/jsTree.css" />
      <script type="text/javascript" src="/lib/js/jquery.js"></script>
      <script type="text/javascript" src="/lib/js/jquery.jstree.js"></script>
      <script type="text/javascript">
         var jsonString = {attr:{id:"n0", rel: "root"}, data: "{%$TR->meccano->sitetree%}", state: "open", children: {%$jsonString%}};
         $(document).ready(function(){
            jQuery("#tree").jstree({
               "animation": 50,
               'plugins': ['themes','json_data','contextmenu','dnd','types'],
               'themes': {
                  'theme' : 'classic'
               },
               'json_data': {
                  'data': jsonString,
                  "ajax" : {
                     "url": "/admin/tree.php" ,
                     "data" : function (n) {
                        return { id : $(n).attr ? $(n).attr("id").substring(1) : 0 };
                     }
                  }
               },
               'contextmenu': {
                  items: function(n){
                     if($(n).attr('id') == 'n0')
                     {
                        cm = {
                           interf: {
                              label: "{%$categories[7]->title|escape:'javascript'%}",
                              action: function(){
                                 parent.document.getElementById('content').src = '/admin/interface.html';
                              },
                              icon: "/tpa/img/16/fields.png"
                           },
                           create: {
                              label: "{%$TR->meccano->addcategory|escape:'javascript'%}",
                              action: function(){
                                 parent.document.getElementById('content').src = '/admin/category/0/new.html';
                              },
                              icon: "/tpa/img/16/folder_add.png"
                           }
                        }
                     } else
                     {
                        var type = $(n).attr('id').substring(0,1);
                        var id = $(n).attr('id').substring(1);
                        var cm = {
                           open: {
                              label: "{%$TR->meccano->open|escape:'javascript'%}",
                              action: function(){
                                 parent.document.getElementById('content').src = '/admin/category/' + id + '.html';
                              },
                              icon: "/tpa/img/16/"+ (type == "n" ? "folder_edit.png" : "page_edit.png")
                           }
                        };
                        if(type == 'c') {
                           cm.create = {
                              label: "{%$TR->meccano->addcategory|escape:'javascript'%}",
                              action: function(){
                                 parent.document.getElementById('content').src = '/admin/category/' + id + '/new.html';
                              },
                              icon: "/tpa/img/16/folder_add.png"
                           };
                           cm.interf = {
                              label: "{%$categories[7]->title|escape:'javascript'%}",
                              action: function(){
                                 parent.document.getElementById('content').src = '/admin/interface' + (id != 0 ? '/'+id : '') + '.html';
                              },
                              icon: "/tpa/img/16/fields.png"
                              // TODO: пофиксить иконку для пункта контекстного меню - интерфейс
                           };
                           cm.addart = {
                              label: "{%$TR->meccano->addarticle|escape:'javascript'%}",
                              action: function(){
                                 parent.document.getElementById('content').src = '/admin/article/' + id + '/new.html';
                              },
                              icon: "/tpa/img/16/page_add.png"
                           };
                        } else if(type == 'a')
                        {
                           cm.copy = {
                              label: "{%$TR->meccano->copy|escape:'javascript'%}",
                              icon: "/tpa/img/16/doc_copy.png"
                           }
                        }
                        {%if $bind_enabled%}
                        cm.linked = {
                           label: "{%$TR->meccano->linking|escape:'javascript'%}",
                           action: function(){
                              var title = type == 'a' ? $('a', n).attr('title') : $.trim($(n).text());
                              if(typeof parent.frames['content'].AddInField_bind == 'function')
                              {
                                 params = {id: id, folder: type, title: title};
                                 parent.frames['content'].AddInField_bind(params);
                              } else
                              {
                                 alert($('#errorBinding').text());
                              }
                           },
                           icon: "/tpa/img/16/link.png"
                        };
                        {%/if%}
                        cm.del = {
                           label: "{%$TR->meccano->delete|escape:'javascript'%}",
                           action: function(n){
                              var alertText = $('#delTreeConfirm').text();
                              if(window.confirm(alertText.replace('%s', $.trim($(n).text()))) == false) return false;
                              $.ajax({
                                 type: "POST",
                                 url: '/admin/tree/delete.html',
                                 data: {
                                    type: $(n).attr ? $(n).attr("id").substring(0, 1) : '',
                                    id: $(n).attr ? $(n).attr("id").substring(1) : 0
                                 },
                                 dataType: "json",
                                 success:function(data){
                                    if(data)
                                    {
                                       $('#tree').jstree("delete_node", n);
                                    }
                                 }
                              });
                           },
                           icon: "/tpa/img/16/delete.gif"
                        };
                     }

                     return cm;
                  }
               },
               types: {
                  "types": {
                     "root": {
                        icon: {image: "/tpa/img/tree/d.png", position: "-19px -90px"},
                        close_node: true
                     },
                     "article": {
                        valid_children: "none",
                        icon: {image: "/tpa/img/tree/d.png", position: "-3px -73px"}
                     },
                     "article-del": {
                        valid_children: "none",
                        icon: {image: "/tpa/img/tree/d.png", position: "-3px -90px"}
                     },
                     "article-binded": {
                        valid_children: "none",
                        icon: {image: "/tpa/img/tree/d.png", position: "-76px -90px"}
                     },
                     "article-more": {
                        valid_children: "none",
                        icon: {image: "/tpa/img/tree/d.png", position: "-2px -106px"}
                     },
                     "folder-del": {
                        icon: {image: "/tpa/img/tree/d.png", position: "-57px -54px"}
                     },
                     "folder-exe": {
                        icon: {image: "/tpa/img/tree/d.png", position: "-57px -72px"}
                     },
                     "folder-empty": {
                        icon: {image: "/tpa/img/tree/d.png", position: "-76px -38px"}
                     },
                     "folder-del-empty": {
                        icon: {image: "/tpa/img/tree/d.png", position: "-38px -72px"}
                     },
                     "folder-exe-empty": {
                        icon: {image: "/tpa/img/tree/d.png", position: "-20px -72px"}
                     }
                  }
               }
            }).bind("move_node.jstree", function (e, data) {
               $.ajax({
                  async: false,
                  type: 'POST',
                  url: '/admin/tree/move.html',
                  data: {
                     "node": $(data.rslt.o).attr("id"),
                     "parent": $(data.rslt.np).attr("id"),
                     "position": data.rslt.p,
                     "relate": $(data.rslt.r).attr("id")
                  },
                  success: function(r){
                     if(r == false)
                     {
                        $.jstree.rollback(data.rlbk);
                     }
                  },
                  error: function(){
                     $.jstree.rollback(data.rlbk);
                  }
               });
            });

         });

         function UpdateNode(node)
         {
            if(typeof node == 'object')
            {
               if(node.parent)
               {
                  $("#tree").jstree("refresh", '#n'+node.parent);
               } else
               {
                  $("#tree").jstree("refresh", '#n0');
               }
            }
         }
      </script>
   </head>
   <body>
      <div id="tree"></div>
      
      <div style="display:none;" id="delTreeConfirm">{%$TR->meccano->delconfirm%}</div>
      <div style="display:none;" id="errorBinding">{%$TR->error->binding%}</div>
   </body>
</html>
